﻿var ChineseTransformLen = ChineseTransformSS.length;
var ChineseTransformLen2 = ChineseTransformSS2.length;
var ChineseTransformG2B = new Object();
var ChineseTransformB2G = new Object();
var ChineseTransformStep = 2000;
var ChineseTransformDelay = 10;

for(var i = 0; i < ChineseTransformLen; i++)
{
    var ctf_s = ChineseTransformSS[i];
    var ctf_t = ChineseTransformTT[i];
    ChineseTransformB2G[ctf_t] = ctf_s;
}

for(var j = 0; j < ChineseTransformLen2; j ++)
{
    var ctf_s2 = ChineseTransformSS2[j];
    var ctf_t2 = ChineseTransformTT2[j];
    ChineseTransformG2B[ctf_s2] = ctf_t2;
}

function ChineseTransform()
{
    var doc0 = null;
    var des0 = null;
    var src0 = null;
    var index0 = 0;
    var state0 = 0;
    var matrix = 0;
}

ChineseTransform.prototype.Init = function(doc, mat)
{
    this.doc0 = doc;
    this.matrix = mat;
    this.src0 = this.doc0.body.innerHTML;
    this.des0 = '';
    this.index0 = 0;
    this.state0 = 0;
};

ChineseTransform.prototype.Run = function()
{
    for(var i = this.index0; this.index0 < i + ChineseTransformStep && this.index0 < this.src0.length; this.index0++)
    {
	var ch = this.src0.charAt(this.index0);
	if(ch >= '\u4e00')
	{
	    var se = this.matrix[ch];
	    if(se != undefined)
	    {
		if(this.state0 == 0)
		{
		    this.des0 += this.src0.substring(this.des0.length, this.index0);
		    this.state0 = 1;
		}
		this.des0 += se;
	    }
	    else
	    {
		this.state0 = 0;
	    }
	}
	else
	{
	    this.state0 = 0;
	}
    }
    var temp = this;
    if(this.index0 < this.src0.length)
    {
	setTimeout(function(){temp.Run();}, ChineseTransformDelay);
    }
    else
    {
	this.des0 += this.src0.substring(this.des0.length, this.index0);
	this.doc0.body.innerHTML = this.des0;
    }
};

function ChineseG2B()
{
    var doc = content.document;
    var trans = new ChineseTransform();
    trans.Init(doc, ChineseTransformG2B);
    trans.Run();
}

function ChineseB2G()
{
    var doc = content.document;
    var trans = new ChineseTransform();
    trans.Init(doc, ChineseTransformB2G);
    trans.Run();
}

function OnlineProxyBreak()
{
    var alpha1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var alpha2 = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
    var alnum  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._';

    function base64_encode(str)
    {
  	var out = '';
	var t, x, y ,z;

	for (var i = 0; i < str.length; i += 3)
	{
            t = Math.min(3, str.length - i);
            if (t == 1)
            {
		x = str.charCodeAt(i);
		out += alnum.charAt((x >> 2));
		out += alnum.charAt(((x & 0X00000003) << 4));
		out += '--';
            }
            else if (t == 2)
            {
		x = str.charCodeAt(i);
		y = str.charCodeAt(i+1);
		out += alnum.charAt((x >> 2));
		out += alnum.charAt((((x & 0X00000003) << 4) | (y >> 4)));
		out += alnum.charAt(((y & 0X0000000f) << 2));
		out += '-';
            }
            else
            {
		x = str.charCodeAt(i);
		y = str.charCodeAt(i+1);
		z = str.charCodeAt(i+2);
		out += alnum.charAt((x >> 2));
		out += alnum.charAt((((x & 0x00000003) << 4) | (y >> 4)));
		out += alnum.charAt((((y & 0X0000000f) << 2) | (z >> 6)));
		out += alnum.charAt((z & 0X0000003f));
            }
	}

	return out;
    }

    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    prefs = prefs.getBranch("extensions.gb2big.");

    var proxy_server = prefs.getCharPref('proxy-server');
    var proxy_option = prefs.getCharPref('proxy-option');

    var url1 = content.document.location.href;
    var url2 = document.getElementById('urlbar').value;

    if(url2 != '')var newurl = base64_encode(url2);
    else var newurl = base64_encode(url1);

    //var resulturl = 'http://proxylord.com/proxy.php?q='+newurl+'&hl=6a';
    var resulturl = proxy_server + newurl + '&' + proxy_option;

    content.document.location.href = resulturl;
}

